package org.example.realtime.jtp.serv.jtpservicerealtime.controller;

import com.alibaba.fastjson.JSON;
import org.example.realtime.jtp.serv.jtpservicerealtime.bean.AppStartCount;
import org.example.realtime.jtp.serv.jtpservicerealtime.bean.KeyCount;
import org.example.realtime.jtp.serv.jtpservicerealtime.bean.TrafficUvReport;
import org.example.realtime.jtp.serv.jtpservicerealtime.service.TrafficChannelStatsService;
import org.example.realtime.jtp.serv.jtpservicerealtime.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author Lianzy
 * @Package org.example.realtime.jtp.serv.jtpservicerealtime.controller
 * @Date 2025/5/21 15:30
 */
@RestController
@RequestMapping("/jtp/traffic")
public class TrafficController {
    private static final String DATE_DEFAULT_VALUE = "1970-07-01";
    /**
     * 自动装载渠道流量统计服务实现类
     */
    @Autowired
    private TrafficChannelStatsService trafficChannelStatsService;
    /**
     * 唯一访客请求拦截方法
     */
    @RequestMapping("/UvCount")
    public String getUvCount(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE) String date){
        //请求参数日期 初步判断 默认为当天日期
        if(DATE_DEFAULT_VALUE.equals(date)){
            date= DateUtils.nowDate();
        }
        //请求服务层查询数据
        List<TrafficUvReport> trafficUvReportList = trafficChannelStatsService.getUvCount(date);
        //如果没有数据直接返回空字符串
        if (null==trafficUvReportList || trafficUvReportList.isEmpty()){
            return "[]";
        }
        return JSON.toJSONString(trafficUvReportList);
    }


    /**
     * 关键词统计
     */
    @RequestMapping("/KeyCount")
    public String keycount(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE) String date){
        //请求参数日期 初步判断 默认为当天日期
        if(DATE_DEFAULT_VALUE.equals(date)){
            date= DateUtils.nowDate();
        }
        //请求服务层查询数据
        List<KeyCount> keycountlist = trafficChannelStatsService.keycount(date);
        //如果没有数据直接返回空字符串
        if (null==keycountlist || keycountlist.isEmpty()){
            return "[]";
        }
        return JSON.toJSONString(keycountlist);
    }


    @RequestMapping("/Startcount")
    public String Startcount(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE) String date){
        //请求参数日期 初步判断 默认为当天日期
        if(DATE_DEFAULT_VALUE.equals(date)){
            date= DateUtils.nowDate();
        }
        //请求服务层查询数据
        List<AppStartCount> startcount = trafficChannelStatsService.Startcount(date);
        //如果没有数据直接返回空字符串
        if (null==startcount || startcount.isEmpty()){
            return "[]";
        }
        return JSON.toJSONString(startcount);
    }
}
